$(function () {
    // 修改两个select的无数据时反馈字符串
    $("#postFirstPetCategories").select2({
        language: {
            noResults: function (params) {
                return "无数据";
            }
        }
    });
    $("#postSecondPetCategories").select2({
        language: {
            noResults: function (params) {
                return "无数据";
            }
        }
    });

    // 宠物分类数据
    $.ajax({
        type: 'GET',
        url: '/firstPetCategories',
        success: function (result) {
            let postFirstPetCategories = $('#postFirstPetCategories');
            let postSecondPetCategories = $('#postSecondPetCategories');
            // 先清空之前的分类
            postFirstPetCategories.empty();
            postSecondPetCategories.empty();
            if (result.statusCode === 200) {
                // 一级分类
                selectCreate(postFirstPetCategories, result.data);
                // 二级分类
                selectCreate(postSecondPetCategories, result.data[0].secondPetCategories);
            }
        }
    });

    // 二级分类变化
    $('#postFirstPetCategories').on('change', function () {
        let categoryId = $(this).find('option:selected').val();
        $.ajax({
            type: 'GET',
            url: '/secondPetCategories',
            data: {categoryId: categoryId},
            success: function (result) {
                // 二级分类
                let postSecondPetCategories = $('#postSecondPetCategories');
                // 先清空之前的二级分类
                postSecondPetCategories.empty();
                if (result.statusCode === 200) {
                    selectCreate(postSecondPetCategories, result.data);
                }
            }
        });
    });
});